import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/utils/date_tools.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

void showDatetimePicker({
  required BuildContext context,
  void Function(DateTime)? onchange,
  DateTime? minimumDate,
  DateTime? initialDateTime,
  DateTime? maximumDate,
  CupertinoDatePickerMode? mode,
  String? format,
}) {
  DateTime dateTime = initialDateTime ?? DateTime.now();

  showCupertinoModalPopup(
    context: context,
    builder: (BuildContext context) => Container(
      height: 246,
      padding: const EdgeInsets.only(top: 6.0),
      margin: EdgeInsets.only(
        bottom: MediaQuery.of(context).viewInsets.bottom,
      ),
      color: CupertinoColors.systemBackground.resolveFrom(context),
      child: SafeArea(
        top: false,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  GestureDetector(
                    onTap:() {
                      Navigator.pop(context);
                    },
                    child: const Text("取消"),
                  ),
                  GestureDetector(
                    onTap:() {
                      // DateTools.formatTime(dateTime.millisecondsSinceEpoch, format: format ?? "{y}-{m}-{d}")
                      if (onchange != null) onchange(dateTime);
                      Navigator.pop(context);
                    },
                    child: const Text("确定", style: TextStyle(color: ThemeVars.colorPrimary),),
                  ),
                ],
              ),
            ),
            SizedBox(
              width: MediaQuery.of(context).size.width,
              height: 216,
              child: CupertinoDatePicker(
                mode: mode ?? CupertinoDatePickerMode.date,
                initialDateTime: dateTime,
                minimumDate: minimumDate ?? DateTime(1970, 1, 1),
                maximumDate: maximumDate ?? DateTime(2035, 12, 31),
                minimumYear: 1970,
                maximumYear: 2035,
                use24hFormat: true,
                // This shows day of week alongside day of month
                showDayOfWeek: false,
                dateOrder: DatePickerDateOrder.ymd,
                // This is called when the user changes the date.
                onDateTimeChanged: (DateTime newDate) {
                  dateTime = newDate;
                },
              ),
            ),
          ],
        ),
      ),
    ),
  );
}